package com.zry.study.animation

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseViewHolder
import com.zry.study.R
import com.zry.study.glide.GlideLoadPhoto
import com.zry.study.utils.FlowTagView
import org.greenrobot.eventbus.EventBus



/**
 * Created by zry on 2017/12/8.
 */

class MyBaseViewHolder(view: View) : BaseViewHolder(view) {
	/**
	 * 设置字体加粗效果
	 */
	fun setTextBold(viewId: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		val paint = view.paint
		paint.isFakeBoldText = true
		return this
	}
	
	/**
	 * 取消字符加粗效果
	 */
	fun setTextBoldNull(viewId: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		val paint = view.paint
		paint.isFakeBoldText = false
		return this
	}
	
	/**
	 * 设置item中textView中个别字体的颜色
	 */
	fun setTextSingleColor(context: Context, viewId: Int, value: CharSequence, tempStr: String, color: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		val fstart = value.indexOf(tempStr)
		val fend = fstart + tempStr.length
		val style = SpannableStringBuilder(value)
		style.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, color)), fstart, fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
		view.text = style
		return this
	}
	
	
	/**
	 * 设置item中textView中个别字体的颜色并加粗
	 */
	fun setTextSingleBgColor(context: Context, viewId: Int, value: CharSequence, tempStr: String, color: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		val fstart = value.indexOf(tempStr)
		val fend = fstart + tempStr.length
		val style = SpannableStringBuilder(value)
		style.setSpan(BackgroundColorSpan(ContextCompat.getColor(context, color)), fstart, fend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) //设置背景
		view.text = style
		
		
		return this
	}
	
	/**
	 * 设置recyclerview中的字体颜色
	 */
	fun setTextColorContext(context: Context, viewId: Int, textColor: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		view.setTextColor(ContextCompat.getColor(context, textColor))
		return this
	}
	
	/**
	 * 设置TextView的背景颜色
	 */
	fun setTextBackgroundContext(context: Context, viewId: Int, textColor: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		view.setBackgroundColor(ContextCompat.getColor(context, textColor))
		return this
	}
	
	/**
	 * 设置网络获取的圆形图片
	 */
	fun setImgCircle(context: Context, viewId: Int, imgUrl: Any, photoName: String, errorImg: Int): MyBaseViewHolder {
		val view = getView<ImageView>(viewId)
		GlideLoadPhoto().GlideCircle(context, imgUrl, photoName, view, errorImg)
		return this
	}
	
	/**
	 * 设置网络获取的圆角图片
	 */
	fun setImgRound(context: Context, viewId: Int, imgUrl: Any, photoName: String, errorImg: Int, roundDp: Int): MyBaseViewHolder {
		val view = getView<ImageView>(viewId)
		GlideLoadPhoto().GlideRound(context, imgUrl, photoName, roundDp, view, errorImg)
		return this
	}
	
	/**
	 * 通用加载图片
	 */
	fun setImg(context: Context, viewId: Int, imgUrl: Any, photoName: String, errorImg: Int): MyBaseViewHolder {
		val view = getView<ImageView>(viewId)
		GlideLoadPhoto().GlideImg(context, imgUrl, photoName, view, errorImg)
		return this
	}
	
	/**
	 * 只加载图片
	 */
	
	fun setLoadImg(context: Context, imgUrl: Any, viewId: Int): MyBaseViewHolder {
		val view = getView<ImageView>(viewId)
		GlideLoadPhoto().GlideLoadImg(context, imgUrl, view)
		return this
	}
	
	
	
	/**
	 * 设置TextView的高度
	 */
	fun setTextHeight(viewId: Int, heightInt: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		val layoutParams = view.layoutParams as RelativeLayout.LayoutParams
		layoutParams.height = heightInt
		view.layoutParams = layoutParams
		return this
	}
	
	/**
	 * 设置TextView的宽度
	 */
	fun setTextWidth(viewId: Int, widthInt: Int): MyBaseViewHolder {
		val view = getView<TextView>(viewId)
		val layoutParams = view.layoutParams as RelativeLayout.LayoutParams
		layoutParams.width = widthInt
		view.layoutParams = layoutParams
		return this
	}
	
	/**
	 * 设置recyclerView中控件的高度
	 */
	fun setLinearLayoutHeight(viewId: Int, heightInt: Int): MyBaseViewHolder {
		val view = getView<View>(viewId)
		val params = view.layoutParams as LinearLayout.LayoutParams
		params.height = heightInt
		view.layoutParams = params
		return this
	}
	
	/**
	 * 设置recyclerView中设置控件的宽度
	 */
	fun setLinearLayoutWidth(viewId: Int, widthInt: Int): MyBaseViewHolder {
		val view = getView<View>(viewId)
		
		val params = view.layoutParams as LinearLayout.LayoutParams
		params.width = widthInt
		view.layoutParams = params
		return this
	}
	
	/**
	 * 设置recyclerView中RelativeLayout的高度
	 */
	fun setRelativeLayoutHeight(viewId: Int, heightInt: Int): MyBaseViewHolder {
		val view = getView<View>(viewId)
		val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams
		layoutParams.height = heightInt
		view.layoutParams = layoutParams
		return this
	}
	
	/**
	 * 设置recyclerView中RelativeLayout的宽度
	 */
	fun setRelativeLayoutWidth(viewId: Int, widthInt: Int): MyBaseViewHolder {
		val view = getView<View>(viewId)
		val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams
		layoutParams.width = widthInt
		view.layoutParams = layoutParams
		return this
	}
	
	/**
	 * 给FlowTagView设置内容
	 */
	fun setFlowTagView(context: Context, viewId: Int, viewString: Array<String>): MyBaseViewHolder {
		val tagView = getView<FlowTagView>(viewId)
		tagView.datas(viewString)
			//下面的5个方法若不设置，则会采用默认值
			.textColor(ContextCompat.getColor(context, R.color.textGrayColor), ContextCompat.getColor(context, R.color.textColor)).textSize(sp2px(15, context)).backgroundColor(ContextCompat.getColor(context, R.color.textGrayColor), ContextCompat.getColor(context, R.color.textGrayColor)).itemHeight(dp2px(30, context)).padding(dp2px(10, context), dp2px(10, context), dp2px(15, context))
			//上面的5个方法若不设置，则会采用默认值
			.listener { view, position ->
//				EventBus.getDefault().post(EventBusBean.EventBusFirst(position, viewString[position]))
			}
			//commit必须调用
			.commit()
		return this
	}
	
	private fun sp2px(sp: Int, context: Context): Int {
		val density = context.resources.displayMetrics.scaledDensity
		return (sp * density + 0.5f).toInt()
	}
	
	private fun dp2px(dp: Int, context: Context): Int {
		val density = context.resources.displayMetrics.density
		return (dp * density + 0.5f).toInt()
	}
	
	
	/**
	 * 设置子recyclervice的布局
	 */
//	fun setSonRecyclerViewAdapter(context: Context, imageAdapter: AppAdapter01.ImageAdapter, viewId: Int,itemCount:Int): MyBaseViewHolder {
//		val recyclerView = getView<RecyclerView>(viewId)
//		val gridLayoutManager = GridLayoutManager(context, itemCount)
//		gridLayoutManager.orientation = GridLayoutManager.VERTICAL
//		recyclerView.layoutManager = gridLayoutManager
//		recyclerView.isNestedScrollingEnabled = false //禁止嵌套滑动
//		recyclerView.adapter = imageAdapter
//		return this
//	}
}


